%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CS221 Programming Assignment 2
%%   Olga Russakovsky, Oct. 2009
%%   Stanford University
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Given an imageFilename (e.g., 'training-1k-images.idx3') and a
% labelsFilename (e.g., 'training-1k-labels.idx1'), parses both files and
% returns a struct DigitSet:
%       DigitSet.pixels is a K x N matrix of pixel values, where K is the
%       number of example images and N is the number of pixels within each
%       example image 
%       DigitSet.labels is a K-dimensional vector of example labels (0-9)
%       DigitSet.weights is a K-dimensional vector of example weights

function [DataSet, DataTestSet]= loadData(filename)
    data = load(filename);
   totalnumex = length(data(:,1));
   disp(sprintf('Total number of examples : %d',totalnumex));
   
    % put 2/3 of examples in dataset
    DataSet.numExamples = floor(totalnumex*2/3);
    DataSet.numFeatures = length(data(1,:))-1;
    DataSet.y = data(1:DataSet.numExamples, end); % last column is y (fall/nofall)
    DataSet.x = data(1:DataSet.numExamples, 1:(end-1));
    DataSet.weights = ones(DataSet.numExamples, 1);
    disp(sprintf('Training data loaded with %d examples, %d features', DataSet.numExamples, DataSet.numFeatures));

   % put 1/3 of examples in datatestset
    DataTestSet.numExamples = totalnumex - DataSet.numExamples;
    DataTestSet.numFeatures = length(data(1,:))-1;
    DataTestSet.y = data((DataSet.numExamples+1):end, end); % last column is y (fall/nofall)
    DataTestSet.x = data((DataSet.numExamples+1):end, 1:(end-1));
    DataTestSet.weights = ones(DataTestSet.numExamples, 1);
    disp(sprintf('Testing  data loaded with %d examples, %d features', DataTestSet.numExamples, DataTestSet.numFeatures));
   
    
    % size checks
    if (length(DataSet.y)~=DataSet.numExamples)
        error('wrong number of examples');
    end
    if(length(DataSet.x(:,1))~=DataSet.numExamples)
        error('wrong number of examples in features');        
    end
    if (length(DataSet.x(1,:))~=DataSet.numFeatures)
            error('wrong number of features');
    end
end